<?php
	if(!MB_admin){
		exit();
	}
	$group = new MBAdminGroup($host,$root,$pwd,$db);
	$rets = $group->showGroup(true);

?>
	<form id="modForm" name="modform" method="post" action="admin_group_act.php?a=mod" class="table_header">
		<div class="mods">
			<label>模块名称：</label>用户分组
		</div>
		<div><cite>对用户分组进行操作</cite></div>
	</form>
	<div class="table_header">
		<strong>已添加分组</strong>
		<cite> &nbsp; 已添加的分组如下表显示，加粗部分为当前生效的分组。</cite>
		<span><input type="button" value="新建分组" class="buttonA" id="newModule"/></span>
	</div>
	<?php if($rets['count']>0){?>
	<table width="96%" border="0" cellspacing="0" cellpadding="0" align="center" class="tableA">
	  <tr>
		<th>组名</th>
		<th width="100">有效性</th>
		<th width="100">优先级</th>
		<th width="150">添加时间</th>
		<th width="200">操作</th>
	  </tr>
	  <?foreach($rets['list'] as $key => $b):?>
	  <tr <?if(!$b['gstatus'] ){echo 'style="font-weight:600;"';}?>>
		<td><?php echo htmlencode($b['gname'])?></td>
		<td><?php if($b['gstatus']){echo '无效';}else{echo '有效';}?></td>
		<td><?php echo $b['gpriority']?></td>
		<td><?php echo date('Y-m-d H:i', $b['gadd_time'])?></td>
		<td><a href="javascript:void(0);" rel="showUser" rev="<?php echo $b['gid'];?>">查看用户</a> <a href="javascript:void(0);" rel="edit" rev="<?php echo $key;?>">编辑</a> <a href="javascript:void(0);" rel="del" rev="<?php echo $b['gid'];?>">删除</a></td>
	  </tr>
	  <?endforeach?>
	</table>
	<?php }else{?>
		<div class="nodata">目前还没有分组！</div>
	<?php }?>
<?php
	$pages = new MBPage('admin_group.php?cid='.$cindex,$rets['count'],$p,10);
	$pages->showpage();
?>
<script type="text/javascript" >
	var _mlength = <?php echo count($rets['list']);?>;
	function delobj(i){
		$.post('admin_group_act.php?a=gp&m=del',{id:i},function(d){
			eval('r='+d);
			if(r){
				cwin.alert('操作成功！');
			}else{
				cwin.alert('操作成功！');
			}
		});	
	}	

	var _data = [];	
	<?php if($rets['count']>0){?>
		<?php foreach($rets['list'] as $key => $b){?>
        _data[<?php echo $b['gid'];?>] = {
			'gid': <?php echo $b['gid'];?>,
			'gname': '<?php echo htmlencode($b['gname']);?>',
			'gstatus': <?php echo $b['gstatus'];?>,
			'gpriority': <?php echo $b['gpriority'];?>
		}
		<?php }?>
	<?php }?>

	var _html = [
		'<form id="dataForm" name="form1" method="post" action="admin_group_act.php?a=gp&m=add">',
		'	<ul class="form">',
		'		<li><strong>分组名：</strong><input id="gname" type="text"  name="gname" style="width:140px;" class="txt"/></li>',
		'		<li><strong>状态：</strong><input type="radio" name="status" value="0" id="status_o" class="txt" checked=""/><label for="status_o">开启</label> &nbsp; <input type="radio" name="status" value="1" id="status_c" class="txt"/><label for="status_c">关闭</label> &nbsp; </li>',
		'		<li><strong>排序：</strong><input id="priority"  type="text" name="priority" style="width:140px;" class="txt" value="9"/></li>',
		'		<li><strong></strong>查询结果按照升序排序</li>',
		'		<li><strong></strong><input type="submit" value="确定" class="button"/> <input type="reset" value="取消" class="button closeBtn"/><input type="hidden" value="" name="gid" id="gid" /></li>',		
		'</form>'
	];

	var cwin=new moduleObj();
	cwin.autoResize(420,200);
	cwin.hide();


	$("#newModule").click(function(){
		if($('#dataForm').length == 0){
			cwin.show({title:"添加话题",text:_html.join('')});
			$('#dataForm').validate({
					rules:{
						gname:{
							required:true,
							maxlength:10
						},
						priority:{
							required:true,
							range:[0,999]
						}
					},
					messages:{
						gname:{
							required:'请填写分组名',
							maxlength:'话题名不能超过10个字'
						},
						priority:{
							required:'请选择排序0-999',
                            range:'顺序在0-999之间'
						}
					}
				});			
		}else{
			$("#dataForm").attr('action','admin_group_act.php?a=gp&m=add');
			$('#dataForm label.error').hide();
			$('#gname').val('');	
			$('#gT').val('');
			$('#priority').val('');
			$('#pT').val('');
			cwin.show();
		}
	});

	$('a[rel=showUser]').click(function(){
		window.location.href = 'admin_group.php?cid=1&gid='+$(this).attr('rev')
	});	


	$('a[rel=edit]').click(function(){
		var no = $(this).attr('rev');

		if($('#dataForm').length == 0){
			cwin.show({title:"修改话题",text:_html.join('')});
			$("#dataForm").attr('action','admin_group_act.php?a=gp&m=edit');
			$('#gname').val(_data[no].gname);	
			$('#priority').val(_data[no].gpriority);
			$('#gid').val(_data[no].gid);
            if(_data[no].gstatus)
            {
                 $('#status_c').attr('checked',true)
                $('#status_o').attr('checked',false)
            }else{
                $('#status_o').attr('checked',true)
                 $('#status_c').attr('checked',false)
            }
			$('#dataForm').validate({
					rules:{
						gname:{
							required:true,
							maxlength:10
						},
						priority:{
							required:true,
							range:[0,999]
						}
					},
					messages:{
						gname:{
							required:'请填写分组名',
							maxlength:'话题名不能超过10个字'
						},
						priority:{
							required:'请选择排序0-999',
                            range:'顺序在0-999之间'
						}
					}
				});			
		}else{
			cwin.show({title:"修改话题"});
			$("#dataForm").attr('action','admin_group_act.php?a=gp&m=edit');
			$('#gname').val(_data[no].gname);
			$('#priority').val(_data[no].gpriority);
			$('#gid').val(_data[no].gid);
            if(_data[no].gstatus)
            {
                 $('#status_c').attr('checked',true)
                $('#status_o').attr('checked',false)
            }else{
                $('#status_o').attr('checked',true)
                 $('#status_c').attr('checked',false)
            }
 
			$('#addArea').fadeIn("slow");				
		}		

	});

	$('a[rel=del]').click(function(){
		var no = $(this).attr('rev');
		cwin.config('确定要删除此项?','delobj('+no+')');			
	});	


</script>
